/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import DAO.LocationDao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import model.Location;

/**
 *
 * @author William
 */
public class LocationControl {

    static EntityManagerFactory emf = Persistence.createEntityManagerFactory("tpi-liqPU");
    LocationDao locationDao;
    EntityManager em;

    public LocationControl() {
        emf = Persistence.createEntityManagerFactory("tpi-liqPU");
    }

    public void create(final Location location) {
        em = emf.createEntityManager();
        em.getTransaction().begin();
        locationDao.create(location, em);
        em.close();
    }

    public Location search(final Long id) {
        Location location;
        em = emf.createEntityManager();
        em.getTransaction().begin();
        location = locationDao.search(id, em);
        em.close();
        return location;
    }

    public void update(final Location location) {
        em = emf.createEntityManager();
        em.getTransaction().begin();
        locationDao.update(location, em);
        em.close();
    }

    public void delete(final Long Id) {
        final Location location = search(Id);
        if (location != null) {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            locationDao.delete(location, em);
            em.close();
        }

    }
}
